<!--
 * @Description: 辅料选择(单选)
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-07-04 16:11:53
 * @FilePath: /yuxiuproweb/src/views/material/other/mods/radioPart.vue
-->
<template>
  <el-dialog
    :close-on-click-modal="false"
    title="辅料选择"
    :visible.sync="shows"
    width="1060px"
    append-to-body
  >
    <div class="part">
      <div class="part-left">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
        >
          <el-form-item label="品名" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入品名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <el-table ref="multipleTable" :data="fabricList" :max-height="312">
          <el-table-column width="60" align="center">
            <template slot-scope="{ row }">
              <el-radio
                v-model="row.checked"
                class="radio-view"
                :label="true"
                @change="checkedChange(row)"
                ><i></i
              ></el-radio>
            </template>
          </el-table-column>
          <el-table-column label="布绑" prop="name">
            <template slot-scope="{ row }">
              <el-link type="primary" @click="setImage(row)">{{
                row.name
              }}</el-link>
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="unitName">
            <template slot-scope="{ row }">
              {{
                getTypeName(dict.type.md_other_material_unit_name, row.unitName)
              }}
            </template>
          </el-table-column>
          <el-table-column label="是否启用" prop="isEnable">
            <template slot-scope="{ row }">{{
              row.isEnable === "Y" ? "是" : "否"
            }}</template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
      <div class="part-right ml10">
        <el-image :src="skuPicture">
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"></i>
          </div>
        </el-image>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="shows = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listMaterial } from "@/api/material/other";
export default {
  dicts: ["md_other_material_unit_name"],
  props: {
    show: Boolean,
    type: {
      type: String,
      default: "add",
    },
    data: {
      type: Object,
    },
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        name: null,
      },
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 面料维护表格数据
      fabricList: [],
      skuPicture: "",
      multipleSelection: [],
      checkedRow: null,
    };
  },
  computed: {
    shows: {
      get() {
        return this.show;
      },
      set(newValue) {
        this.$emit("update:show", newValue);
      },
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询面料维护列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then(({ rows, total }) => {
        this.fabricList = rows?.map((e) => ({
          ...e,
          checked: this.checkedRow?.otherMaterialId === e.otherMaterialId,
        }));
        this.total = total;
        this.loading = false;
      });
    },
    /** 显示右侧图片 */
    setImage({ skuPicture }) {
      this.skuPicture = skuPicture;
    },
    checkedChange(row) {
      this.fabricList?.forEach((e) => {
        if (e.otherMaterialId !== row.otherMaterialId) e.checked = false;
      });
      this.checkedRow = {
        ...row,
      };
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    /** 提交按钮 */
    submitForm() {
      if (this.checkedRow) {
        this.$emit("on-change", this.checkedRow);
        this.shows = false;
      } else {
        this.$message.error("请选择布绑");
      }
    },
  },
};
</script>

<style scoped lang="scss">
.part {
  width: 100%;
  display: flex;
  &-left {
    width: 600px;
    overflow: hidden;
  }
  &-right {
    flex: 1;
    border: 1px solid rgba(220, 220, 220, 0.5);
    border-radius: 4px;
    padding: 10px;
    img {
      width: 400px;
      height: 400px;
    }
    ::v-deep {
      .image-slot {
        width: 400px;
        height: 400px;
        font-size: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #909399;
      }
    }
  }
}
</style>
